// misTS.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include "TSServiceSoap.nsmap"
#include "soapH.h"
#include <stdio.h>

#define progname	"mists.exe"
#define	version		"1.0.0"
#define DEBUG		1

void usage(void)
{
	fprintf(stderr, "Transfer Error to Trouble Shooting Shopfloor(VS2017)   Version: %s\n"
		"\n(c) Tody 2018, T-ware Inc.\n"
		"\nUsage:\n\t%s -url http://10.100.160.37:888/TSService.asmx -sn <Serialnumber> -err <Errcode> -TID <SITEID> -des <content>"
		"\nOptions:\n"
		"      -url <website>       website must be assigned\n"
		"      -Err <Errcode>       Error code for issue\n"
		"      -Des <content>       Detail information for issue\n"
		"      -TID <SITEID>        Terminal ID for LINE Trouble shooting\n"
		"      -sn  <Serialnumber>  Error SN to Repair MES\n", version, progname);
	exit(1);
}

int main(int argc, char **argv)
{
	struct soap						*soap;
	struct _tsN__Auto_USCORETS_USCOREStage ErrInfo;
	struct _tsN__Auto_USCORETS_USCOREStageResponse ErrInfoRes;

	char *website=NULL;
	char realweb[512];
	int retCode = 0;

	if (argc >= 11 && strcmp("-URL", _strupr(argv[1])) == 0
		&& strcmp("-SN", _strupr(argv[3])) == 0
		&& strcmp("-ERR", _strupr(argv[5])) == 0
		&& strcmp("-TID", _strupr(argv[7])) == 0
		&& strcmp("-DES", _strupr(argv[9])) == 0)
	{
		if(strstr(argv[2], "http://")){
			website = argv[2];
			memset(realweb, 0, 512);
			strcpy(realweb, argv[2]);
			website = realweb;
		}else{
			website = NULL; 		/*using default website.*/
		}
	}
	else {
		website = NULL;
		usage();
	}

	soap = soap_new();

	/* fill with information and ready to upload */
	ErrInfo.SN = argv[4];
	ErrInfo.ErrorCode = argv[6];
	ErrInfo.Terminal_USCOREID = argv[8];
	ErrInfo.Key = argv[10];

	printf("Trying to upload <%s> with <%s> to Trouble Shooting System...", ErrInfo.SN = argv[4], ErrInfo.ErrorCode = argv[6]);

	/* function call here and check result */
	soap_call___tsN2__Auto_USCORETS_USCOREStage(soap, website, NULL, &ErrInfo, &ErrInfoRes);
	if (soap->error){
		printf(" Failed\n\n");
		soap_print_fault(soap, stderr);
		retCode = 1;
	} else {
		printf("\nMES: %s", ErrInfoRes.Auto_USCORETS_USCOREStageResult);
	}

	printf("\nReturn code: %d", retCode);
	/* destory soap connection */
	soap_destroy(soap);
	soap_end(soap);
	soap_done(soap);
	return retCode;
}
